﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NHibernateDB;

namespace MessMain
{
    public partial class LoginForm : Form
    {
        private string limba;
        private string port;
        private string ip;
        public LoginForm(string ip, string port, string limba)
        {
            InitializeComponent();
            Rename(limba);


            this.ip = ip;
            this.port = port;
            this.limba = limba;
        }

        private void btLogin_Click(object sender, EventArgs e)
        {
            if (tbName.Text.Length == 0 && mtbPass.Text.Length == 0)
            {
                XMLParser parser = new XMLParser(@"C:\Language" + limba + ".xml");

                Dictionary<string, string> dict =
                    parser.ExtractAttributePairs("MesajeEroare", string.Empty, string.Empty, "name", "formula");

                MessageBox.Show(dict["MessCamp"],
                                dict["TitluEroare"],
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }
            else
            {
                PersistenceManager pm = new PersistenceManager();
                IList<Login> loginID = pm.RetrieveEquals<Login>("Nume", tbName.Text);

                System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
                byte[] data = System.Text.Encoding.ASCII.GetBytes(mtbPass.Text);
                data = x.ComputeHash(data);

                bool equal = true;
                for (int i = 0; i < data.Length; i++)
                {
                    if (data[i] != loginID[0].Password[i])
                        equal = false;
                }

                if (equal == true)
                {
                    Program.name = tbName.Text;
                    //Comunication com = new Comunication(ip, port, limba, tbName.Text);
                    //Hide();
                    //com.ShowDialog();
                    //com.Focus();
                    this.Close();
                }
                else
                {
                    string xmlFile = @"C:\Language" + limba + ".xml";
                    XMLParser xmlParser = new XMLParser(xmlFile);
                    Dictionary<string, string> errors = xmlParser.ExtractAttributePairs("MesajeEroare", string.Empty, string.Empty, "name", "formula");
                    MessageBox.Show(errors["ErrorUser"], "Error",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void btLost_Click(object sender, EventArgs e)
        {
            string xmlFile = @"C:\Language" + limba + ".xml";
            XMLParser xmlParser = new XMLParser(xmlFile);
            Dictionary<string, string> emailContent = xmlParser.ExtractAttributePairs("Email", string.Empty, string.Empty, "name", "formula");
            Dictionary<string, string> errors = xmlParser.ExtractAttributePairs("MesajeEroare", string.Empty, string.Empty, "name", "formula");

            PersistenceManager pm = new PersistenceManager();
            IList<Login> loginID = pm.RetrieveEquals<Login>("Nume", tbName.Text);

            string email = loginID[0].Email;
            try
            {
                pm.Delete<Login>(loginID[0]);
            }catch
            {
                MessageBox.Show(errors["ErrorUser"], "Error",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            
            /*
            System.Web.Mail.MailMessage message = new System.Web.Mail.MailMessage();
            message.From = "stan_adi@yahoo.com";
            message.To = email;
            message.Subject = emailContent["Subject"];
            message.Body = emailContent["Body"];
            System.Web.Mail.SmtpMail.SmtpServer = "mail.rdstm.ro";
            try
            {
                System.Web.Mail.SmtpMail.Send(message);
            }catch
            {
                MessageBox.Show(errors["SMTPError"], "Error",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }*/

            

            System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] data = System.Text.Encoding.ASCII.GetBytes("MedicalDB_11");
            data = x.ComputeHash(data);
           

            Login newUser = new Login();
            newUser.Nume = tbName.Text;
            newUser.Password = data;
            newUser.Email = email;

            try
            {
                pm.Save(newUser);
            }catch
            {
                MessageBox.Show(errors["ErrorUser"], "Error",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }


            MessageBox.Show(errors["PassMod"], "Error",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);

        }
    }
}
